package com.wxtwxt.ruralmall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.wxtwxt.ruralmall.common.utils.R;
import com.wxtwxt.ruralmall.ware.feign.MemberFeignService;
import com.wxtwxt.ruralmall.ware.vo.FareVo;
import com.wxtwxt.ruralmall.ware.vo.MemberReceiveAddressVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxtwxt.ruralmall.common.utils.PageUtils;
import com.wxtwxt.ruralmall.common.utils.Query;

import com.wxtwxt.ruralmall.ware.dao.WareInfoDao;
import com.wxtwxt.ruralmall.ware.entity.WareInfoEntity;
import com.wxtwxt.ruralmall.ware.service.WareInfoService;

import javax.annotation.Resource;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Resource
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isNotEmpty(key)) {
            queryWrapper.eq("id", key).or().like("name", key).or().like("address", key).or().like("areacode", key);
        }
        IPage<WareInfoEntity> page = this.page(new Query<WareInfoEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public FareVo getFare(Long addrId) {
        R r = memberFeignService.info(addrId);
        if(r.getCode() == 0) {
            FareVo fareVo = new FareVo();
            MemberReceiveAddressVo memberReceiveAddress = r.getData("memberReceiveAddress", new TypeReference<MemberReceiveAddressVo>() {});
            String phone = memberReceiveAddress.getPhone();
            BigDecimal fare = new BigDecimal(phone.substring(phone.length() - 1));
            fareVo.setMemberReceiveAddressVo(memberReceiveAddress);
            fareVo.setFare(fare);
            return fareVo;
        }
        return null;
    }

}